/**
 * Created by dukes on 2015/12/4.
 */
//module game{
    class ChatScene extends eui.Component{
        // private inited:boolean = false;
        // private blackListRender:eui.IFactory;
        // private personalChatListRender:eui.IFactory;
        // public curType:number = -1;
        // private provider:eui.ArrayCollection;
        public constructor(){
            super();
            this.skinName = "skins.scene.ChatSceneSkin";
            this.addEventListener(egret.Event.ADDED_TO_STAGE, this.addedToStage, this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removedFromStage, this);
        }

        public updaterProvider(){
            // if(this.curType == 1){
            //     this.provider.source = ChatMgr.getInstance().chatList;
            // }else if( this.curType == 2 ){
            //     this.provider.source = ChatMgr.getInstance().blockList;
            // }
        }
        /**
         * 0 世界
         * 1 系统
         * 2 私聊
         * 3 黑名单
         * -1 强制刷新，不改变当前type
         * */
        public updateType(type:number=-1){
            // this.curType = type;
            // this.chat_world_n.visible = type != 0;
            // this.chat_world_s.visible = type == 0;
            // this.chat_personal_n.visible = type != 1;
            // this.chat_personal_s.visible = type == 1;
            // this.chat_black_list_n.visible = type != 2;
            // this.chat_black_list_s.visible = type == 2;
            // this.message_area.visible = type == 0 ;
            // this.fuck_list.visible = type == 1 || type == 2;
            // if(type == 1){
            //     this.fuck_list.itemRenderer = this.personalChatListRender;
            //     this.provider.source = ChatMgr.getInstance().chatList;
            // }else if( type == 2 ){
            //     this.fuck_list.itemRenderer = this.blackListRender;
            //     this.provider.source = ChatMgr.getInstance().blockList;
            // }
            // this.group_num.visible = this.group_total.visible = this.black_list_remind.visible = type == 2;
            // this.input_group.visible = type == 0;
            // //if( type == 3){
            //     //GetUIAssetsNum(Model.userExt.blockNum,this.group_num);
            //     //GetUIAssetsNum(Model.userExt.maxBlockNum,this.group_total);
            //     this.updateBlockRelated();
            // //}
            // ChatPopPanel.getInstance().hide();
            // if( this.curType == 1)
            //     RedPointMgr.getInstance().update(RedPointMgr.CHAT_TAB,false);
        }


        public updateBlockRelated(){
            // if( this.curType == 2){
            //     this.provider.source = ChatMgr.getInstance().blockList;
            //     this.group_num.text = this.provider.source.length+"";
            //     this.group_total.text = Model.userExt.maxBlockNum+"";
            //     //GetUIAssetsNum(this.provider.source.length,this.group_num);
            //     //GetUIAssetsNum(Model.userExt.maxBlockNum,this.group_total);
            // }
        }

        public onMessageBack(){
            // if( this.curType == 0){
            //     this.message_area.textFlow = ChatMgr.getInstance().worldChatInfoList;
            // }
            // //else if( this.curType == 1){
            // //    this.message_area.textFlow = ChatMgr.getInstance().systemInfoList;
            // //}
            // this.message_area.commitProperties();
            // this.message_area.height = this.message_area._textField.textHeight;
            // var sv = this.message_area.height - 620;
            // if( sv > 0){
            //     this.messageGroup.verticalScrollPosition = sv;
            //     this.scroller._updateContentPosition();
            // }
        }

        public updateChatList(){
            // if( this.curType == 1){
            //     this.fuck_list.itemRenderer = this.personalChatListRender;
            //     this.provider.source = ChatMgr.getInstance().chatList;
            // }
        }

        private onSelectedTab(evt:egret.Event) {
            // SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            // this.updateType(evt.newIndex);
            // if( this.curType == 0 ){//|| this.curType == 1){
            //     this.onMessageBack();
            // }

        }

        private onReturn(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            SceneMgr.getInstance().switchScene(SceneMgr.SCENE_MAIN);
        }

        private onSend(evt:egret.TouchEvent){
            SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            evt.stopImmediatePropagation();
            var gap = Math.floor((Model.SYSTIME - Model.LastChatTime)/1000);
            if( gap < 10)
            {
                ShowHint((10-gap)+"秒后可继续发言");
                return;
            }
            var text:string = this.input_msg.text.trim();
            if( text.length != 0){
                window["__sendToWorld"](text);
                this.input_msg.text = "";
            }
        }
        private onText(evt:egret.TextEvent){
            // SoundMgr.getInstance().playEffect(SoundMgr.BTN);
            // var data = evt.text.split("&");
            // if( data.length >= 2){
            //     var userId:number = parseInt(data[0]);
            //     if( userId && userId != Model.me.id)
            //         ChatPopPanel.getInstance().show({userId:userId,name:data[1]},evt.stageX,evt.stageY);
            // }
        }
        public forceDisplay(data){
            this.chat_tab.selectedIndex = 1;
            this.updateType(1);
        }
        private addedToStage(evt:egret.Event){
            // if( !this.inited){
            //     this.inited = true;
            //     this.blackListRender = new eui.ClassFactory(BlackListItemRender);
            //     this.personalChatListRender = new eui.ClassFactory(PersonalChatItemRender);
            //     this.chat_tab.itemRenderer = new eui.ClassFactory(ChatTabButton);
            //     this.chat_tab.dataProvider = new eui.ArrayCollection([0,1,2]);
            //     this.provider = new eui.ArrayCollection();
            //     this.fuck_list.dataProvider = this.provider;
            // }
            // this.chat_tab.addEventListener(eui.IndexChangeEvent.CHANGE, this.onSelectedTab, this);
            // this.img_return.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onReturn, this);
            // this.img_send.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSend, this);
            // this.message_area._textField.addEventListener(egret.TextEvent.LINK, this.onText, this);
            // if( this.curType == -1){
            //     this.chat_tab.selectedIndex = 0;
            //     this.updateType(0);
            //     this.onMessageBack();
            // }
            // if( !ChatMgr.getInstance().blockList)
            //     window["__blocks"]();
            // this.hind_personal_chat.visible = RedPointMgr.getInstance().getState(RedPointMgr.CHAT_TAB);
            //for(var i = 0; i < 60; i++){
            //    ChatMgr.getInstance().addWorldChatData({userId:100+i,name:"hehe"+i,type:i%2+1,message:"dfgdgdsf"+i});
            //}
        }

        private removedFromStage(evt:egret.Event){
            // this.chat_tab.removeEventListener(eui.IndexChangeEvent.CHANGE, this.onSelectedTab, this);
            // this.img_return.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onReturn, this);
            // this.img_send.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSend, this);
            // this.message_area._textField.removeEventListener(egret.TextEvent.LINK, this.onText, this);
        }

        private chat_world_n:eui.Image;
        private chat_world_s:eui.Image;
        private chat_personal_n:eui.Image;
        private chat_personal_s:eui.Image;
        private chat_black_list_n:eui.Image;
        private chat_black_list_s:eui.Image;

        public fuck_list:eui.List;
        private message_area:eui.Label;
        private black_list_remind:eui.Image;
        private img_return:eui.Image;
        private input_group:eui.Group;
        private input_msg:eui.TextInput;
        private img_send:eui.Image;
        private hind_personal_chat:eui.Image;
        private chat_tab:eui.TabBar;
        private group_num:eui.BitmapLabel;
        private group_total:eui.BitmapLabel;
        private scroller:eui.Scroller;
        private messageGroup:eui.Group;
        public partAdded(name:string, instance:any){
            super.partAdded(name, instance);
            if( name == "img_return"){
                this.img_return = instance;
            }else if( name == "chat_tab"){
                this.chat_tab = instance;
            }else if( name == "chat_world_n"){
                this.chat_world_n = instance;
            }else if( name == "chat_world_s"){
                this.chat_world_s = instance;
            }else if( name == "chat_personal_n"){
                this.chat_personal_n = instance;
            }else if( name == "chat_personal_s"){
                this.chat_personal_s = instance;
            }else if( name == "chat_black_list_n"){
                this.chat_black_list_n = instance;
            }else if( name == "chat_black_list_s"){
                this.chat_black_list_s = instance;
            }else if( name == "message_area"){
                this.message_area = instance;
                // this.message_area._textField.touchEnabled = true;
            }else if( name == "black_list_remind"){
                this.black_list_remind = instance;
            }else if( name == "input_group"){
                this.input_group = instance;
            }else if( name == "input_msg"){
                this.input_msg = instance;
            }else if( name == "img_send"){
                this.img_send = instance;
            }else if( name == "fuck_list"){
                this.fuck_list = instance;
            }else if( name == "group_num"){
                this.group_num = instance;
                this.group_num.textAlign = "center";
            }else if( name == "group_total"){
                this.group_total = instance;
                this.group_total.textAlign = "center";
            }else if( name == "scroller"){
                this.scroller = instance;
            }else if( name == "messageGroup" ){
                this.messageGroup = instance;
            }else if(name == "hind_personal_chat"){
                this.hind_personal_chat = instance;
                RedPointMgr.getInstance().registerPoint(RedPointMgr.CHAT_TAB,instance);
            }
        }

        private static _instance:ChatScene;
        public static getInstance():ChatScene{
            if( ChatScene._instance == null)
                ChatScene._instance = new ChatScene();
            return ChatScene._instance;
        }
    }
//}